@echo off
setlocal enabledelayedexpansion

:: Set qsb tool path
@REM set QSB_PATH="D:\qt\Tools\QtCreator\bin\qsb.exe"
set QSB_PATH="D:\MySoftwares\QTCreator17\Tools\QtCreator\bin\qsb.exe"


echo ======================================
echo      Fragment Shader Compiler
echo ======================================
echo.

:: Check if qsb tool exists
if not exist %QSB_PATH% (
    echo Error: qsb tool not found
    echo Path: %QSB_PATH%
    echo Please check Qt Creator installation path
    pause
    exit /b 1
)

:: Search for .frag files
echo Searching for .frag files in current directory...
echo.

set count=0
for %%f in (*.frag) do (
    set /a count+=1
    echo [!count!] %%f
)

if %count%==0 (
    echo No .frag files found in current directory
    pause
    exit /b 0
)

echo.
echo Found %count% .frag files
echo.
echo Compilation options:
echo [A] Compile all files
echo [S] Select specific files
echo [Q] Quit
echo.
set /p choice="Enter your choice (A/S/Q): "

if /i "%choice%"=="Q" exit /b 0
if /i "%choice%"=="A" goto compile_all
if /i "%choice%"=="S" goto select_files

echo Invalid choice, exiting...
pause
exit /b 1

:compile_all
echo.
echo Starting compilation of all files...
echo.
for %%f in (*.frag) do (
    call :compile_file "%%f"
)
goto summary

:select_files
echo.
echo Enter file numbers to compile (space separated, e.g: 1 3 5):
set /p selection="Numbers: "

for %%i in (%selection%) do (
    set current=0
    for %%f in (*.frag) do (
        set /a current+=1
        if !current!==%%i (
            call :compile_file "%%f"
        )
    )
)
goto summary

:compile_file
set file=%~1
echo Compiling: %file%

@REM %QSB_PATH% --glsl "100 es,120,150" --hlsl 50 --msl 12 -o "%~n1.frag.qsb" %file%
%QSB_PATH% -g --glsl "100 es,120,150" --hlsl 50 --msl 12 -o "%~n1.frag.qsb" %file%

if errorlevel 1 (
    echo   [FAILED] %file% compilation failed
    set /a failed+=1
) else (
    echo   [SUCCESS] Generated: %~n1.frag.qsb
    set /a success+=1
)
echo.
goto :eof

:summary
echo ================================
echo Compilation complete!
echo Success: %success% files
echo Failed: %failed% files
echo ================================

if %failed% gtr 0 (
    echo.
    echo Note: Some files failed to compile, please check .frag syntax
)

echo.
echo Press any key to exit...
pause >nul